package com.ruoyi.buss.task.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.buss.task.domain.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.buss.task.service.IDataTaskInfoService;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;

/**
 * 营销任务Controller
 *
 * @author sxy
 * @date 2021-07-16
 */
@RestController
@RequestMapping("/taskInfo")
public class DataTaskInfoController extends BaseController {
	@Autowired
	private IDataTaskInfoService dataTaskInfoService;


	@Autowired
	private TokenService tokenService;

	@PreAuthorize(hasPermi = "buss:taskInfo:relationTaskCustomer")
	@GetMapping("/relationTaskCustomer")
	public AjaxResult relationTaskCustomer() {
		dataTaskInfoService.relationTaskCustomer();
		return AjaxResult.success();
	}


	/**
	 * 查询营销任务详情
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:item")
	@GetMapping("/item")
	public AjaxResult item(String taskId) {
		LoginUser userInfo = tokenService.getLoginUser();
		DataTaskInfo taskInfoVO = dataTaskInfoService.item(taskId, userInfo);
		return AjaxResult.success(taskInfoVO);
	}

	/**
	 * 查询营销任务列表(taskType=0查总行，taskType=1查个人)
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:list")
	@GetMapping(value = "/list")
	public TableDataInfo taskList(DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		List<DataTaskInfo> list = dataTaskInfoService.taskList(dataTaskInfo, userInfo);
		return getDataTable(list);
	}
	/**
	 * 总行任务导出
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:exportZh")
	@Log(title = "营销任务", businessType = BusinessType.EXPORT)
	@PostMapping("/exportZh")
	public void exportZh(HttpServletResponse response, DataTaskInfo dataTaskInfo) throws IOException {
		LoginUser userInfo = tokenService.getLoginUser();
		List<DataTaskInfo> list = dataTaskInfoService.taskList(dataTaskInfo, userInfo);
		ExcelUtil<DataTaskInfo> util = new ExcelUtil<DataTaskInfo>(DataTaskInfo.class);
		util.exportExcel(response, list, "总行任务导出");

	}
	/**
	 * 新增 指定客户 营销任务 包含一键有效
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:insert")
	@PostMapping("/insert")
	public AjaxResult insert(@RequestBody DataTaskInfo dataTaskInfo) {
		System.err.println(dataTaskInfo.toString());
		LoginUser userInfo = tokenService.getLoginUser();
		return AjaxResult.success(dataTaskInfoService.insertTask(dataTaskInfo, userInfo));
	}

	/**
	 * 修改营销反馈
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:updateExInfo")
	@PostMapping("/updateExInfo")
	public AjaxResult updateExInfo(@RequestBody DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		return AjaxResult.success(dataTaskInfoService.updateExInfo(dataTaskInfo, userInfo));
	}

	/**
	 * 检验是否可以开始营销
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:ExRecord")
	@GetMapping("/ExRecord")
	public AjaxResult ExRecord(DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		return AjaxResult.success(dataTaskInfoService.ExRecord(dataTaskInfo, userInfo));
	}

	/**
	 * 查询营销任务对应的客户列表
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:listCustom")
	@GetMapping("/listCustom")
	public TableDataInfo listCustom(DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		return getDataTable(dataTaskInfoService.listCustom(dataTaskInfo, userInfo));
	}

	/**
	 * 查询营销任务对应的客户列表
	 *
	 * @param dataTaskInfo
	 * @param 
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:taskListCustom")
	@GetMapping("/taskListCustom")
	public TableDataInfo taskListCustom(DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		List<DataTaskInfo>  list = dataTaskInfoService.taskListCustom(dataTaskInfo, userInfo);
		return getDataTable(list);
	}

	/**
	 * 添加营销反馈
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:insertTaskExRecord")
	@PostMapping("/insertTaskExRecord")
	public AjaxResult insertTaskExRecord(@RequestBody DataTaskExRecord dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		return AjaxResult.success(dataTaskInfoService.insertTaskExRecord(dataTaskInfo, userInfo));
	}

	/**
	 * 开始营销任务
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:startTaskExRecord")
	@PostMapping ("/startTaskExRecord")
	public AjaxResult startTaskExRecord(@RequestBody  DataTaskExRecord dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		return AjaxResult.success(dataTaskInfoService.startTaskExRecord(dataTaskInfo, userInfo));
	}

	/**
	 * 查询营销反馈列表
	 * @param taskID
	 * @param idCard
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:listTaskExRecord")
	@GetMapping("/listTaskExRecord")
	public TableDataInfo listTaskExRecord(String taskID,
										  String idCard) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		return getDataTable(dataTaskInfoService.listTaskExRecord(taskID, idCard, userInfo));
	}

	/**
	 * 查询营销任务报表信息
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:queryReports")
	@GetMapping("/queryReports")
	public TableDataInfo queryReports(DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		List<TaskReportsVO> list = dataTaskInfoService.queryReports(dataTaskInfo, userInfo);
		return getDataTable(list);
	}

	/**
	 * 根据taskId对表DATA_TASK_INFO、DATA_PLAN_TASK_RELATION、DATA_USER_TASK_RELATION进行删除字段操作
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:delete")
	@PostMapping("/delete")
	public AjaxResult delete(@RequestBody DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		return dataTaskInfoService.delete(dataTaskInfo, userInfo);
	}

	/**
	 * 任务退回，针对已办结任务
	 *
	 * @param taskCustomerVO
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:taskReturn")
	@PostMapping("/taskReturn")
	public AjaxResult dataTaskInfoService(@RequestBody DataTaskInfo taskCustomerVO) {
		LoginUser userInfo = tokenService.getLoginUser();
		return dataTaskInfoService.taskReturn(taskCustomerVO, userInfo);
	}

	/**
	 * 营销客户列表
	 *
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:marketTaskList")
	@GetMapping("/marketTaskList")
	public TableDataInfo marketTaskList(DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage("A.UPDATE_TIME desc");
		List<DataTaskInfo> list = dataTaskInfoService.marketTaskList(dataTaskInfo, userInfo);
		return getDataTable(list);
	}


	/**
	 * 营销客户列表
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:exportMarketTaskList")
	@Log(title = "营销任务", businessType = BusinessType.EXPORT)
	@PostMapping("/exportMarketTaskList")
	public void exportMarketTaskList(HttpServletResponse response, ExprotDataTaskInfo dataTaskInfo) throws IOException {
		LoginUser userInfo = tokenService.getLoginUser();
		List<ExprotDataTaskInfo> list = dataTaskInfoService.exprotMarketTaskList(dataTaskInfo, userInfo);
		ExcelUtil<ExprotDataTaskInfo> util = new ExcelUtil<ExprotDataTaskInfo>(ExprotDataTaskInfo.class);
		util.exportExcel(response, list, "营销客户列表导出");

	}

	/**
	 * 获取任务完成情况
	 *
	 * @param dataTaskInfo 0是任务数 1是到个人
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:getMarketStatus")
	@GetMapping("/getMarketStatus")
	public TableDataInfo getMarketStatus(DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		List<DataTaskInfo> list = dataTaskInfoService.getMarketStatus(dataTaskInfo, userInfo);
		return getDataTable(list);
	}

	/**
	 * 新增营销反馈的图片
	 *
	 * @param
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:updateExImage")
	@PostMapping("/updateExImage")
	public AjaxResult updateExImage(@RequestBody DataTaskExRecordImage dataTaskExRecordImage) {
		LoginUser userInfo = tokenService.getLoginUser();
		return dataTaskInfoService.updateExImage(dataTaskExRecordImage, userInfo);
	}

	/**
	 * 删除营销反馈的图片
	 *
	 * @param dataTaskExRecordImage
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:delExImage")
	@PostMapping("/delExImage")
	public AjaxResult delExImage(@RequestBody DataTaskExRecordImage dataTaskExRecordImage) {
		LoginUser userInfo = tokenService.getLoginUser();
		return dataTaskInfoService.delExImage(dataTaskExRecordImage, userInfo);
	}
	/**
	 * 删除任务/客户
	 * @param dataTaskInfo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:delTaskInfo")
	@PostMapping("/delTaskInfo")
	public AjaxResult delTaskInfo(@RequestBody DataTaskInfo dataTaskInfo) {
		LoginUser userInfo = tokenService.getLoginUser();
		return dataTaskInfoService.delTaskInfo(dataTaskInfo, userInfo);
	}

	/**
	 * 营销任务客户经理分配
	 * 针对根据自定义查询生成的营销任务，对其中查询到没有管户的客户进行分配
	 * 客户分配类型必选，主要分为存量客户、网格村居、网格商户、网格社区、网格专业市场
	 * @param userTaskRelationDO
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:queryAllotTaskCustomer")
	@GetMapping("/queryAllotTaskCustomer")
	public TableDataInfo queryAllotTaskCustomer(UserTaskRelationDO userTaskRelationDO) {
		if (StringUtils.isNotEmpty(userTaskRelationDO.getOnlyUserId())) {
			userTaskRelationDO.setOrgId(null);
			userTaskRelationDO.setUserId(userTaskRelationDO.getOnlyUserId());
		}

		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		List<UserTaskRelationDO> list = dataTaskInfoService.queryAllotTaskCustomer(userTaskRelationDO, userInfo);
		return getDataTable(list);
	}

	/**
	 *
	 * @param vo
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:allotTaskCustomer")
	@PostMapping("/allotTaskCustomer")
	public AjaxResult AllotTaskCustomer(@RequestBody UserTaskRelationDO vo) {
		if (StringUtils.isEmpty(vo.getOrgId())) {
			throw new CustomException("机构号不能为空");
		}
		if (StringUtils.isEmpty(vo.getUserId())) {
			throw new CustomException("客户经理号不能");
		}
		LoginUser userInfo = tokenService.getLoginUser();
		return dataTaskInfoService.AllotTaskCustomer(vo, userInfo);
	}
	/**
	 * 查询营销任务列表
	 */
//    @PreAuthorize(hasPermi = "buss:taskInfo:list")
//    @GetMapping("/taskList")
//    public TableDataInfo list(DataTaskInfo dataTaskInfo)
//    {
//        startPage();
//        List<DataTaskInfo> list = dataTaskInfoService.selectDataTaskInfoList(dataTaskInfo);
//        return getDataTable(list);
//    }

	/**
	 * 导出营销任务列表
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:export")
	@Log(title = "营销任务", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, UserTaskRelationDO userTaskRelationDO) throws IOException {
		LoginUser userInfo = tokenService.getLoginUser();
		List<UserTaskRelationDO> list = dataTaskInfoService.queryAllotTaskCustomer(userTaskRelationDO, userInfo);
		ExcelUtil<UserTaskRelationDO> util = new ExcelUtil<UserTaskRelationDO>(UserTaskRelationDO.class);
		util.exportExcel(response, list, "营销任务数据");

	}

	/**
	 * 获取营销任务详细信息
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:query")
	@GetMapping(value = "/{taskId}")
	public AjaxResult getInfo(@PathVariable("taskId") String taskId) {
		return AjaxResult.success(dataTaskInfoService.selectDataTaskInfoById(taskId));
	}

	/**
	 * 新增营销任务
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:add")
	@Log(title = "营销任务", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody DataTaskInfo dataTaskInfo) {
		return toAjax(dataTaskInfoService.insertDataTaskInfo(dataTaskInfo));
	}

	/**
	 * 修改营销任务
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:edit")
	@Log(title = "营销任务", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody DataTaskInfo dataTaskInfo) {
		return toAjax(dataTaskInfoService.updateDataTaskInfo(dataTaskInfo));
	}

	/**
	 * 查询白名单公共池客户列表(taskType=0查总行，taskType=1查个人)
	 *
	 * @param newtableAll
	 * @return
	 */
	@PreAuthorize(hasPermi = "buss:taskInfo:getNewtableAllList")
	@GetMapping(value = "/getNewtableAllList")
	public TableDataInfo getNewtableAllList(NewtableAll newtableAll) {
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		List<NewtableAll> list = dataTaskInfoService.getNewtableAllList(newtableAll, userInfo);
		return getDataTable(list);
	}
}
